﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class NPCAnimationController : MonoBehaviour
{
    private Animator _animator;
    private Rigidbody2D _rb;
    private Player _player;
    private float pauseTime = 0.1f;
    private float _lastPositionX;
    private float scaleX;

    private void Start()
    {
        _animator = GetComponent<Animator>();
        _rb = GetComponent<Rigidbody2D>();

        _animator.SetBool("Moving", false);
        _lastPositionX = 0;
        StartCoroutine(HandleChangeDirection());
        scaleX = transform.lossyScale.x;
    }

    public void SetMovingStatus(bool isMoving)
    {
        _animator.SetBool("Moving", isMoving);
    }

    private void Update()
    {

    }
    private IEnumerator HandleChangeDirection()
    {
        while (true)
        {
            yield return new WaitForSeconds(pauseTime);
            float vX = transform.position.x - _lastPositionX;
            _lastPositionX = transform.position.x;

            var scale = transform.localScale;
            if (vX > 0f)
            {
                scale.x = -Mathf.Abs(scale.x);
            }
            else if (vX < 0f)
            {
                scale.x = Mathf.Abs(scale.x);
            }

            transform.localScale = scale;

        }
    }
}
